/**
 * @author caesar
 * @date 2025/7/2
 * @apiNote
 */
//二分查找
public class binarysearch {
    public static void main(String[] args) {
        int[] arr = {1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

        int target = (int) (Math.random() * 50);
        System.out.println("target = " + target);
        int index = -1;
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            //和中间值相等
            int mid = left + (right-left) / 2;
            if (target == arr[mid]) {
                index = mid;
                break;
            } else if (target > arr[mid]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        if(index != -1){
            System.out.println("找到了，在数组中下标为："+index);
        }else{
            System.out.println("没找到");
        }
    }
}
